$(function () {
    var clientInformationBill_datagrid = $("#clientInformationBill_datagrid");
    var client_datagrid = $("#client_datagrid");
    var clientInformationBill_dialog = $("#clientInformationBill_dialog");
    var clientInformationBill_form = $("#clientInformationBill_form");

    clientInformationBill_datagrid.datagrid({
        fit: true,
        url: "/clientInformationBill/list",
        fitColumns: true,
        striped: true,
        pagination: true,
        rownumbers: true,
        toolbar: "#tb",
        singleSelect: true,
        columns: [[
            {field: "ck", checkbox: true},
            {field: "id", title: "编号", width: 100, hidden: true},
            {
                field: "client", title: "客户姓名", width:100,
                formatter: function (value, row, index) {
                    if (value) {
                        return value.name;
                    } else {
                        return "";
                    }
                }
            },

            //不能只局限于用value取值,当出现冲突的时候要用到列(row)
            //比如在这里:从一个对象中获取多个属性的时候,fileld对应的是类中的字段,不可能出现多个相同,因此用到
            //row.对象名.属性(意思也就是从这一行中的某个对象中,获取到他的属性)
            {
                field: "gender", title: "性别", width:100,
                formatter: function (value, row, index) {
                if (row.client) {
                    if (row.client.gender == 0) {
                        return "女";
                    } else {
                        return "男";
                    }
                } else {
                    return "";
                }
            }
            },
            {
                field: "tel", title: "联系方式", width:100,
                formatter: function (value, row, index) {
                if (row.client) {
                    return row.client.tel;
                } else {
                    return "";
                }
            }
            },
            {
                field: "email", title: "邮箱", width:100,
                formatter: function (value, row, index) {
                if (row.client) {
                    return row.client.email;
                } else {
                    return "";
                }
            }
            },
            {field: "oTime", title: "事故时间", width:100,
                sortable: true},

            {field: "oLocation", title: "事故地点", width: 100},

            {
                field: "guaranteeBill", title: "保险单号", width:100,
                formatter: function (value, row, index) {
                return value.number;
            }
            },
            {
                field: "idNumber", title: "身份证", width:100,
                formatter: function (value, row, index) {
                if (row.client) {
                    return row.client.idNumber;
                } else {
                    return "";
                }
            }
            }
        ]]
    });

    clientInformationBill_dialog.dialog({
        width: 600,
        height: 435,
        closed: true,
        buttons: "#bb",
        title: "查看详情"
    });

    var cmdObj = {
        //工具栏的刷新
        reload: function () {
            clientInformationBill_datagrid.datagrid("reload");
        },
        //关闭
        cancel: function () {
            clientInformationBill_dialog.dialog("close");
        },



        //dialog中插入的datagrid表格
        view:function () {
            //判断是否选中数据
            var row = clientInformationBill_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选择要查看的数据");
                return;
            }
            /*$("#clientInformationBill_form").form("clear");*/
            console.log(row);
            $("#guaranteebill_datagrid").datagrid({
                url: "/accident/selectGuaranteeBillInfo?id="+row.guaranteeBill.id,
                fitColumns: false,
                pagination: true,
                striped: true,
                rownumbers: true,
                singleSelect: true,
                columns: [[
                    {field: "ck", checkbox: true},
                    {field: "id", title: "编号", width: 100, hidden: true},
                    {
                        field: "number", title: "保险单号", width:100,
                        formatter: function (value, row, index) {
                            console.log(row);
                            if (row) {
                                return row.number;
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        field: "beginDate", title: "保险开始日期", width:100,
                        formatter: function (value, row, index) {
                            if (row) {
                                return row.beginDate;
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        field: "endDate", title: "保险截止日期", width:100,
                        formatter: function (value, row, index) {
                            if (row) {
                                return row.endDate;
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        field: "limitYear", title: "保险年限", width:100,
                        formatter: function (value, row, index) {
                            if (row) {
                                return row.limitYear;
                            } else {
                                return "";
                            }
                        }
                    },
                    {
                        field: "totalPrice", title: "保险金额", width:100,
                        formatter: function (value, row, index) {
                            if (row) {
                                return row.totalPrice;
                            } else {
                                return "";
                            }
                        }
                    }

                ]]
            });

            $('#clientInformationBill_dialog').dialog('open');  //弹出这个dialog框
        },

        //高级查询工具
        query: function () {
            //因为easyui对元素的html进行相关封装,可能导致获取不到数据,建议都使用easyui提供获取数据方法
            //var keyword = $("#keyword").val();
            var keyword = $("#keyword").textbox("getValue");
            var beginDate = $("#beginDate").datebox("getValue");
            var endDate = $("#endDate").datebox("getValue");

            //所谓高级查询,无非是让datagrid根据条件去更新列表数据,所以只需要刷新列表,同时携带查询条件即可
            clientInformationBill_datagrid.datagrid("load", {
                keyword: keyword,
                beginDate: beginDate,
                endDate: endDate
            });
        }
    }
    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

});

